﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace Memory_Board
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //Creates an enum that holds the base values of each picture
        enum pics
        { Apple, Umbrella, Orange, Dog, Cat, SmileyFace, House, Banana, Pencil, Stapler }
        List<pics> cards = new List<pics>();
        //Variable Declarations
        string firstCardClicked;
        int order = 0;
        Image display;
        int pairs = 0;
        Button first;
        public int activePlayerNumber = 1;
        int player1score = 0;
        int player2score = 0;
        int player3score = 0;
        int player4score = 0;
        int max = 1;
        BitmapImage arrowSource = new BitmapImage(new Uri("arrow.jpg", UriKind.Relative));
        Window1 menuBoard = new Window1();
        int time = 60;
        DispatcherTimer Timer = new DispatcherTimer();
        

        public MainWindow()
        {
            menuBoard.ShowDialog();
            InitializeComponent();
            //creates 2 of each picture
            for (int i = 0; i < 20; i++)
            {
                cards.Add((pics)(i / 2));
            }
            Pic1.Source = null;
            Pic2.Source = null;
            Pic3.Source = null;
            Pic4.Source = null;
            NewGame();
            max = menuBoard.totalPlayer;

            using (StreamWriter sw = new StreamWriter("IndexLocations.txt", true))
            {
                for (int i = 0; i < 20; i++)
                {
                    sw.WriteLine(cards[i]);
                }
                sw.WriteLine(" ");
            }
            Pic1.Source = arrowSource;

            Timer.Interval = new TimeSpan(0, 0, 1);
            Timer.Tick += Timer_Tick;

            if (max == 1)
            {
                Timer.Start();
            }
        }

        //Converts the pics values to strings
        private String ToImageName(pics picture)
        {
            if (picture == pics.SmileyFace)
            {
                return "smiley face.jpg";
            }
            else
            {
                string returnCardString = picture.ToString();
                return returnCardString + ".jpg";
            }
        }
        private void SetImageSource(Image myImage, int index)
        {
            String stringPath = ToImageName(cards[index]);
            Uri imageUri = new Uri(stringPath, UriKind.Relative);
            BitmapImage imageBitmap = new BitmapImage(imageUri);
            myImage.Source = imageBitmap;
        }
        private void myButtonClicked(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            string buttonName = clickedButton.Name;
            string indexString = buttonName.Replace("button", "");
            string displayName = "display" + indexString;
            int indexNumber = int.Parse(indexString) - 1;
            Button buttonFinder = (Button)this.FindName(buttonName);
            Image displayView = (Image)this.FindName(displayName);

            DoubleAnimation da = new DoubleAnimation();
            da.From = 180;
            da.To = 360;
            da.Duration = new Duration(TimeSpan.FromSeconds(.3));
            RotateTransform rt = new RotateTransform();
            clickedButton.RenderTransform = rt;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);

            string secondCardClicked;
            if (order < 2)
            {
                if (order == 0)
                {
                    //Stores the values at the first clicked button
                    firstCardClicked = ToImageName(cards[indexNumber]);
                    //stores the location of the first button that was clicked
                    first = clickedButton;
                    //stores the image at that button
                    display = displayView;
                    SetImageSource(display, indexNumber);
                    scoreDisplay();
                }
                else if (order == 1)
                {
                    //sets the value for secondCardClicked as the string at index 0
                    secondCardClicked = ToImageName(cards[indexNumber]);
                    //Checks if the strings are equal
                    if (firstCardClicked == secondCardClicked)
                    {
                        if (display == displayView)
                        {
                            MessageBox.Show("You already chose that card please choose another card.");
                            order = 0;
                        }

                        if (display != displayView)
                        {
                            SetImageSource(displayView, indexNumber);
                            MessageBox.Show("You found a match!");
                            displayView.IsEnabled = false;
                            first.IsEnabled = false;
                            clickedButton.IsEnabled = false;
                            pairs++;
                            if (activePlayerNumber == 1)
                            {
                                player1score++;
                            }
                            if (activePlayerNumber == 2)
                            {
                                player2score++;
                            }
                            if (activePlayerNumber == 3)
                            {
                                player3score++;
                            }
                            if (activePlayerNumber == 4)
                            {
                                player4score++;
                            }
                            scoreDisplay();
                            SetImageSource(display, indexNumber);
                        }
                    }
                    else
                    {
                        SetImageSource(displayView, indexNumber);
                        //if the strings don't match it displays the following
                        MessageBox.Show("Not a match");

                        if (activePlayerNumber == 1)
                        {
                            if (activePlayerNumber == max)
                            {
                                activePlayerNumber = 1;
                                MessageBox.Show("It is Player 1's Turn!");
                            }
                            else
                            {
                                activePlayerNumber++;
                                MessageBox.Show("It is Player 2's Turn!");
                                Pic1.Source = null;
                                Pic2.Source = arrowSource;
                            }
                        }
                        else if (activePlayerNumber == 2)
                        {
                            if (activePlayerNumber == max)
                            {
                                activePlayerNumber = 1;
                                MessageBox.Show("It is Player 1's Turn!");
                                Pic2.Source = null;
                                Pic1.Source = arrowSource;
                            }
                            else
                            {
                                activePlayerNumber++;
                                MessageBox.Show("It is Player 3's Turn!");
                                Pic2.Source = null;
                                Pic3.Source = arrowSource;
                            }
                        }
                        else if (activePlayerNumber == 3)
                        {
                            if (activePlayerNumber == max)
                            {
                                activePlayerNumber = 1;
                                MessageBox.Show("It is Player 1's Turn!");
                                Pic3.Source = null;
                                Pic1.Source = arrowSource;
                            }
                            else
                            {
                                activePlayerNumber++;
                                MessageBox.Show("It is Player 4's Turn!");
                                Pic3.Source = null;
                                Pic4.Source = arrowSource;
                            }
                        }
                        else
                        {
                            if (activePlayerNumber == 2)
                            {
                                Pic2.Source = null;
                                Pic1.Source = arrowSource;
                            }
                            else if (activePlayerNumber == 3)
                            {
                                Pic3.Source = null;
                                Pic1.Source = arrowSource;
                            }
                            else
                            {
                                Pic4.Source = null;
                                Pic1.Source = arrowSource;
                            }
                            activePlayerNumber = 1;
                            MessageBox.Show("It is Player 1's Turn!");

                        }
                        scoreDisplay();
                        displayView.Source = null;
                        display.Source = null;
                    }
                }
                order++;
            }
            else
            {
                //if the order value exceeds one it treats it as if the turn starts over
                firstCardClicked = ToImageName(cards[indexNumber]);
                first = clickedButton;
                display = displayView;
                order = 1;
                SetImageSource(display, indexNumber);
            }

            if (pairs == 10)
            {
                if (player1score > player2score && player1score > player3score && player1score > player4score)
                {
                    if (max == 1)
                    {
                        Timer.Stop();
                        string finalTime = string.Format("You found all of the pairs with {0} seconds left!", time);
                        MessageBox.Show(finalTime);
                    }
                    else
                    {
                        MessageBox.Show("The Winner is: Player 1 with a score of " + player1score);
                    }
                }

                if (player2score > player1score && player2score > player3score && player2score > player4score)
                {
                    MessageBox.Show("The Winner is: Player 2 with a score of " + player2score);
                }

                if (player3score > player1score && player3score > player2score && player3score > player4score)
                {
                    MessageBox.Show("The Winner is: Player 3 with a score of " + player3score);
                }

                if (player4score > player1score && player4score > player2score && player4score > player3score)
                {
                    MessageBox.Show("The Winner is: Player 4 with a score of " + player4score);
                }

                if (player1score == player2score && player1score > player3score && player1score > player4score || player1score == player3score && player1score > player2score && player1score > player4score || player1score == player4score && player1score > player3score && player1score > player2score || player2score == player3score && player2score > player1score && player2score > player4score || player2score == player4score && player2score > player1score && player2score > player3score || player3score == player4score && player3score > player1score && player3score > player2score)
                {
                    if (player1score == player2score && player1score > player3score && player1score > player4score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 1 and 2!");
                    }

                    if (player1score == player3score && player1score > player2score && player1score > player4score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 1 and 3!");
                    }

                    if (player1score == player4score && player1score > player2score && player1score > player3score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 1 and 4!");
                    }

                    if (player2score == player3score && player2score > player1score && player2score > player4score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 2 and 3!");
                    }

                    if (player2score == player4score && player2score > player1score && player2score > player3score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 2 and 4!");
                    }

                    if (player3score == player4score && player3score > player1score && player3score > player2score)
                    {
                        MessageBox.Show("There was a Tie! The winners are Player 3 and 4!");
                    }
                }
            }
        }
        private void NewGame_clicked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You have started a new game!");
            NewGame();
            menuBoard.ShowDialog();
            max = menuBoard.totalPlayer;
            MessageBox.Show("It is Player 1's Turn!");
            using (StreamWriter sw = new StreamWriter("IndexLocations.txt", true))
            {
                for (int i = 0; i < 20; i++)
                {
                    sw.WriteLine(cards[i]);
                }
                sw.WriteLine(" ");
            }

            if (max == 1)
            {
                Timer.Start();
            }
            else
            {
                TimeHolder.Text = null;
            }
        }
        private void NewGame()
        {
            var random = new Random();
            //shuffles the cards which creates a random position for each by randomly assigning values to a temporary variable
            for (int x = 0; x < cards.Count; x++)
            {
                pics temp = cards[x];
                int y = random.Next(x, cards.Count);
                cards[x] = cards[y];
                cards[y] = temp;

                for (int i = 1; i <= 20; i++)
                {
                    Button Btn = (Button)this.FindName("button" + i);
                    Btn.IsEnabled = true;
                    Image View = (Image)this.FindName("display" + i);
                    View.Source = null;
                }
                ScoreBox.Text = null;
                ScoreBox2.Text = null;
                ScoreBox3.Text = null;
                ScoreBox4.Text = null;
                pairs = 0;
                player1score = 0;
                player2score = 0;
                player3score = 0;
                player4score = 0;
                activePlayerNumber = 1;
                max = 1;
                Pic1.Source = arrowSource;
                Pic2.Source = null;
                Pic3.Source = null;
                Pic4.Source = null;
                Timer.Stop();
                time = 60;
            }
        }
        private void scoreDisplay()
        {
            ScoreBox.Text = player1score.ToString();
            ScoreBox2.Text = player2score.ToString();
            ScoreBox3.Text = player3score.ToString();
            ScoreBox4.Text = player4score.ToString();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {
                time--;
                TimeHolder.Text = time.ToString(); 
            }

            else
            {
                Timer.Stop();
                MessageBox.Show("Time's Up!");
                string finalScore = string.Format("You were able to find {0} pairs! Try to find all of them next time!", pairs);
                MessageBox.Show(finalScore);
                for (int i = 1; i <= 20; i++)
                {
                    Button Btn = (Button)this.FindName("button" + i);
                    Btn.IsEnabled = false;
                }
            }
        }

        private void Close(object sender, EventArgs e)
        {
            Timer.Stop();
            MessageBox.Show("Thanks for playing!");
        }
   }
}



